<?php
/**
 *ProductPage Model
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * ProductPage_Model
 * Provides method to display the correct content of product pages
 *@package model
 */
class ProductPage_Model extends Model{
    /**
     * Gets text in specific language from database (standard return values / errors)
     * @var object
     */
    private $oTextModel;
    /**
     * Holds connection to database
     * @var object
     */
    protected $oDBInstance;

    /**
     * Model constructor
     * establishes connection to database
     */
    public function __construct(){
        $this->oDBInstance = Mysql_dbconnection::getInstance();
        $this->oTextModel = Loader::loadModel('textLanguage');
    }
    /**
     * Gets the page title which belongs to the product id
     * @param integer product ID
     * @param integer language
     */
    public function getPageTitle($iPage, $iLanguage){
        //prevent from SQL-Injections
        $iLanguage = $this->oDBInstance->getSQLValueString($iLanguage,'int');
        $iPage = $this->oDBInstance->getSQLValueString($iPage,'int');

        //set table for query
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND visible = 1 AND lid = $iLanguage) as a");

        //set column for WHERE clause in query
        $this->oDBInstance->_setDefaultColumn('id');

        //perform query on database
        $this->oDBInstance->performSelect('name', '', '', '', "$iPage");
        //fetch result of query
        $aResult = $this->oDBInstance->fetch('array');

        if(isset($aResult[0]['name'])){
            return $aResult[0]['name'];

        }else{
            return $this->oTextModel->getTextByName('notitle', $iLanguage);
        }

    }
    /**
     * Gets an array of the different product categories
     * @param integer page ID
     * @param integer language
     */
    public function getCategories($iPid, $iLanguage){
        //Sets table (join) for query
        $this->oDBInstance->_setDefaultTable(
            	"(SELECT id, name, content, position, parent, link
            	  FROM bs_page INNER JOIN bs_page_language ON id = pid
            	  WHERE deleted = 0 AND cms_only = 0 AND type = 'category' AND lid = $iLanguage) as a");
        //Sets column for WHERE clause in query
        $this->oDBInstance->_setDefaultColumn('id');
        $sParentID = $this->oDBInstance->getSQLValueString($iPid,'string');
        if ($iPid != ''){
            $this->oDBInstance->performSelect('id, name, content, link','', "a.parent = $sParentID ORDER BY a.position");
            //Fetches the query result in an array
            $aCategories = $this->oDBInstance->fetch('array');
            if(isset($aCategories[0])){
                return $aCategories;
            }else{
                return $this->oTextModel->getTextByName('nocategories', $iLanguage);
            }
        }else{
            return "Es wurde keine Produktseite angegeben.";
        }
    }
    /**
     * Connects to the database and loads all products which belongs to the passed category
     * @param integer category ID
     * @param integer language
     */
    public function getProducts($iCategory, $iLanguage){
        $this->oDBInstance->query("SELECT *
			FROM `bs_product` AS p
			INNER JOIN `bs_product_language` AS l ON p.id = l.pid
			INNER JOIN bs_language as t ON l.lid = t.id INNER JOIN `bs_image` AS i ON p.mainimage = i.id
			WHERE p.category = '$iCategory' AND l.lid = '$iLanguage'ORDER BY p.position DESC;");

        $aProducts = $this->oDBInstance->fetch('array');
        if(isset($aProducts[0])){
            return $aProducts;
        }else{
            return $this->oTextModel->getTextByName('noproducts', $iLanguage);
        }
    }

    /**
     * Loads the product details of a product ID
     * @param integer product ID
     * @param integer language
     */
    public function getProductDetail($iPid, $iLang){
        $this->oDBInstance->query("SELECT p.id, p.name, p.mainimage, p.category, l.short_description, l.long_description, i.link
			FROM `bs_product` AS p
			INNER JOIN `bs_product_language` AS l ON p.id = l.pid
			INNER JOIN bs_image AS i ON p.mainimage = i.id
			WHERE p.id = '$iPid' AND l.lid = '$iLang'
			LIMIT 0 , 30");

        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0])){
            return $aResult[0];
        }else{
            echo "Fehler bei DB-Abfrage von Produktdetails, leeres Resultat!";
            exit();
        }
         

    }

    /**
     * Gets an array of product images which belongs to the product ID
     * @param integer product ID
     */
    public function getProductImages($iPid){
        $this->oDBInstance->query("SELECT i.id, i.link
			FROM `bs_product` AS p
			INNER JOIN bs_image AS i ON  p.id = i.product
			WHERE p.id = '$iPid'
			LIMIT 0 , 30");
        /** $this->oDBInstance->query("SELECT * FROM bs_product WHERE category = '".$sPage."'");**/
         
        return $this->oDBInstance->fetch('array');

    }
    /**
     * Checks if page ID is a category page or an content page
     * @param integer page ID
     */
    public function isCategory($iPage){
        $iProductID = $this->oDBInstance->getSQLValueString($iPage, 'int');
        $this->oDBInstance->query("SELECT type FROM bs_page WHERE id = '$iPage'");

        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0]['type'])){
            if($aResult[0]['type'] == 'category'){
                return TRUE;
            }
        }
        return FALSE;
    }
    /**
     * Gets parent page of categories
     * @param integer category ID
     */
    public function getParentProductPage($iCategory){
        $iCategory = $this->oDBInstance->getSQLValueString($iCategory, 'int');

        $this->oDBInstance->query("SELECT parent FROM bs_page WHERE id = $iCategory");
        $aResult = $this->oDBInstance->fetch('array');
        if(isset($aResult[0]['parent'])){
            return $aResult[0]['parent'];
        }else{
            return FALSE;
        }
    }

}
?>